/*
 * LevelUploadingGameStateController.h
 *
 *  Created on: Apr 6, 2012
 *      Author: erudhes
 */

#ifndef LEVELUPLOADINGGAMESTATECONTROLLER_H_
#define LEVELUPLOADINGGAMESTATECONTROLLER_H_
#include <wx/wx.h>
#include <wx/file.h>
#include "IGameStateController.h"
#include "StaticDrawController.h"
#include "ServerStartingGameStateController.h"
#include "SmartPointer.h"

class wxGauge;

/**
 * This class handles the procedure of uploading a level to the client
 *
 * The uploading starts when the client sends a DOWNLOADING_START packet and finishes when all the needed files are uploaded.
 * During this period this class handles every kind of event.
 * @see IGameStateController
 * @author Rudolf Heszele heszele@gmail.com
 * @version 2.0
 * @since 2012-05-19
 */
class LevelUploadingGameStateController: public IGameStateController
{
	public:
		/**
		 * The only constructor takes the name of the file to start to upload and a ServerStartingController.
		 *
		 * @param fileName The name of the file to start the upload with
		 * @param serverStartingController A ServerStartingController to handle the configuration negotiation at the end
		 */
		LevelUploadingGameStateController(const wxString& fileName, SmartPointer<ServerStartingGameStateController> serverStartingController);
		virtual ~LevelUploadingGameStateController();

		// Methods inherited from Controller

		/**
		 * @see IController::handleMouseDown
		 */
		virtual void handleMouseDown(const wxPoint&);
		/**
		 * @see IController::handleMouseUp
		 */
		virtual void handleMouseUp(const wxPoint&);
		/**
		 * @see IController::handleMouseMotion
		 */
		virtual bool handleMouseMotion(const wxPoint&);
		/**
		 * @see IController::getBitmapToPaint
		 */
		virtual const wxBitmap& getBitmapToPaint() const;
		/**
		 * @see IController::getBitmapToDraw
		 */
		virtual wxBitmap& getBitmapToDraw();

		// Methods inherited from GameStateController

		/**
		 * @see IGameStateContrller::handleSocketConnection
		 */
		virtual void handleSocketConnection(wxSocketEvent&);
		/**
		 * @see IGameStateContrller::handleSocketLost
		 */
		virtual void handleSocketLost(wxSocketEvent& socketEvent);
		/**
		 * @see IGameStateContrller::handleSocketInput
		 */
		virtual void handleSocketInput(wxSocketEvent& socketEvent);
		/**
		 * @see IGameStateContrller::handleSocketOutput
		 */
		virtual void handleSocketOutput(wxSocketEvent&);

	private:
		/**
		 * Enumeration of possible outcomes of upload operations
		 */
		enum UploadError
		{
			NO_ERROR_FINISHED,
			NO_ERROR_DATA_REMAINED,
			FILE_IO_ERROR,
			NETWORK_IO_ERROR,
		};

		/**
		 * Tries to upload the file and handles error scenarios.
		 *
		 * @return true if everything went OK, false otherwise
		 */
		bool uploadFile();

		/**
		 * Uploads one kilobyte from the opened file
		 *
		 * @return NO_ERROR_FINISHED if the whole file is uploaded,
		 *         NO_ERROR_DATA_REMAINED if everything went OK, but data remained to upload
		 *         FILE_IO_ERROR if the file is not opened, or something went wrong during the reading
		 *         NETWORK_IO_ERROR if something went wrong during uploading the data to the client
		 */
		UploadError uploadData();

		/**
		 * This object handles the drawings during this state of the application
		 *
		 * @see StaticDrawController
		 */
		StaticDrawController mStaticDrawController;
		/**
		 * The file object to read the data from the disk
		 */
		wxFile mUploadedFile;
		/**
		 * The length of the file which is under uploading
		 */
		int mUploadedFileLength;
		/**
		 * The number of already uploaded bytes
		 */
		int mUploadedBytes;
		/**
		 * A progress dialog to show the uploading progress to the user
		 */
		wxDialog* mProgressDialog;
		/**
		 * wxWidgets class to show the progress bar itself
		 */
		wxGauge* mGauge;
		/**
		 * The path to the directory the files should be read from
		 */
		wxString mDirectoryPath;
		/**
		 * The name of the file actually uploading
		 */
		wxString mFileName;
		/**
		 * True if one of the files have been successfully uploaded
		 */
		bool mIsUploadedFinished;
		/**
		 * ServerStarting object used to handle the configuration negotiation after the upload is finished
		 */
		SmartPointer<ServerStartingGameStateController> mServerStartingController;
};


#endif /* LEVELUPLOADINGGAMESTATECONTROLLER_H_ */
